package io.jee.caisan.weixin.controller.admin;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import io.jee.alaska.util.Result;
import io.jee.caisan.weixin.domain.MpMaterialFile;
import io.jee.caisan.weixin.domain.MpMaterialNews;
import io.jee.caisan.weixin.domain.MpMaterialNum;
import io.jee.caisan.weixin.service.MpMaterialService;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;

@Controller
@RequestMapping("/admin/mp/material")
public class MpMaterialController {
	
	@Resource
	private WxMpService wxMpService;
	
	@Resource
	private MpMaterialService mpMaterialService;

	@GetMapping
	public String index(Model model) throws WxErrorException {
		MpMaterialNum mpMaterialNum = mpMaterialService.findMpMaterialNumOne();
		List<MpMaterialNews> news = mpMaterialService.listMpMaterialNews();
		List<MpMaterialFile> image = mpMaterialService.listMpMaterialFile(WxConsts.MaterialType.IMAGE);
		List<MpMaterialFile> video = mpMaterialService.listMpMaterialFile(WxConsts.MaterialType.VIDEO);
		List<MpMaterialFile> voice = mpMaterialService.listMpMaterialFile(WxConsts.MaterialType.VOICE);
		model.addAttribute("mpMaterialNum", mpMaterialNum);
		model.addAttribute("news", news);
		model.addAttribute("image", image);
		model.addAttribute("video", video);
		model.addAttribute("voice", voice);
		return "admin/mp/material";
	}
	
	@PostMapping("/async/news")
	@ResponseBody
	public Result<?> asyncNews() {
		mpMaterialService.asyncMaterialNews();
		return Result.success();
	}
	
	@PostMapping("/async/image")
	@ResponseBody
	public Result<?> asyncImage() {
		mpMaterialService.asyncMaterialImage();
		return Result.success();
	}
	
	@PostMapping("/async/video")
	@ResponseBody
	public Result<?> asyncVideo() {
		mpMaterialService.asyncMaterialVideo();
		return Result.success();
	}
	
	@PostMapping("/async/voice")
	@ResponseBody
	public Result<?> asyncVoice() {
		mpMaterialService.asyncMaterialVoice();
		return Result.success();
	}
	
	@PostMapping("/async/result")
	@ResponseBody
	public Result<?> asyncResult() {
		return mpMaterialService.asyncResult();
	}
}
